﻿#region license

// Copyright (c) 2008, Rinat Abdullin (http://rabdullin.com)
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
// 
//     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
//     * Neither the name of the Rinat Abdullin nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Autofac;
using Lokad.Container;

namespace Photon
{
	/// <summary>
	/// Extension module for the <see cref="Autofac"/> 
	/// </summary>
	public sealed class PhotonModule : IModule
	{
		private readonly List<Assembly> _assemblies = new List<Assembly>();

		private readonly List<FileInfo> _files = new List<FileInfo>();

		private static void SplitRun(string value, Action<string> action)
		{
			var split = value.Split(new[] {';'}, StringSplitOptions.RemoveEmptyEntries);

			foreach (var s in split)
			{
				action(s.Trim());
			}
		}

		/// <summary>
		/// <see cref="DecoratorGenerator.AddReference"/>
		/// </summary>
		/// <param name="assembly"></param>
		public void AddReference(Assembly assembly)
		{
			_assemblies.Add(assembly);
		}

		/// <summary>
		/// Adds file path as sources, can use wildcards
		/// </summary>
		/// <param name="path"></param>
		public void AddPath(string path)
		{
			var dir = Path.GetDirectoryName(path);
			var pattern = Path.GetFileName(path);

			if (string.IsNullOrEmpty(dir))
			{
				dir = Directory.GetCurrentDirectory();
			}

			var files = string.IsNullOrEmpty(pattern) ? Directory.GetFiles(dir) : Directory.GetFiles(dir, pattern);
			foreach (var s in files)
			{
				AddFile(s);
			}
		}

		/// <summary>
		/// Same as <see cref="AddPath"/> but is a property for the XML configs
		/// </summary>
		public string IncludePaths
		{
			set { SplitRun(value, AddPath); }
			get { return null; }
		}

		/// <summary>
		/// Same as <see cref="AddFile"/> but is a property for the XML configs
		/// </summary>
		public string IncludeFiles
		{
			set { SplitRun(value, AddFile); }
			get { return null; }
		}

		/// <summary>
		/// Adds a single file
		/// </summary>
		/// <seealso cref="DecoratorGenerator.AddSource"/>
		/// <param name="fileName"></param>
		public void AddFile(string fileName)
		{
			Enforce.ArgumentNotNullOrEmpty(fileName, "fileName");
			_files.Add(new FileInfo(fileName));
		}

		/// <summary>
		/// Same as <see cref="AddReference"/>, but is a property for usage in XML configs
		/// </summary>
		public string IncludeReferences
		{
			set { SplitRun(value, s => AddReference(Assembly.LoadFrom(s))); }
			get { return null; }
		}


		/// <summary>
		/// <see cref="IModule.Configure"/>
		/// </summary>
		/// <param name="container"></param>
		public void Configure(IContainer container)
		{
			var generator = new DecoratorGenerator();

			foreach (var file in _files)
			{
				generator.AddSource(file);
			}

			foreach (var assembly in _assemblies)
			{
				generator.AddReference(assembly);
			}

			container.LoadDecorators(generator.CompileAll());
		}
	}
}